using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using backend.Common;
using backend.Models;

namespace backend.Services
{
    public static class OrderStatusTransitions
    {
        public static readonly Dictionary<OrderStatus, HashSet<OrderStatus>> Transitions = new Dictionary<OrderStatus, HashSet<OrderStatus>>
        {
            { OrderStatus.Unreviewed, new HashSet<OrderStatus> { OrderStatus.Rejected, OrderStatus.WaitingForAssignment } },
            { OrderStatus.Rejected, new HashSet<OrderStatus>() }, // 驳回后无法再改变状态
            { OrderStatus.WithdrawnByUser, new HashSet<OrderStatus>() }, // 撤回后无法再改变状态
            { OrderStatus.WaitingForAssignment, new HashSet<OrderStatus> { OrderStatus.WaitingForRepair } },
            { OrderStatus.WaitingForRepair, new HashSet<OrderStatus> { OrderStatus.Repaired } },
            { OrderStatus.Repaired, new HashSet<OrderStatus>() } // 已维修后无法再改变状态
        };
    }

    public static class UserRolePermissions
    {
        public static readonly Dictionary<UserRole, HashSet<OrderStatus>> Permissions = new Dictionary<UserRole, HashSet<OrderStatus>>
        {
            { UserRole.NormalUser, new HashSet<OrderStatus> { OrderStatus.WithdrawnByUser } },
            { UserRole.RepairPerson, new HashSet<OrderStatus> { OrderStatus.WaitingForRepair } },
            { UserRole.Administrator, new HashSet<OrderStatus> { OrderStatus.Rejected, OrderStatus.WaitingForAssignment, OrderStatus.WaitingForRepair } }
        };
    }

    public static class StatusMapper
    {
        public static readonly Dictionary<string, OrderStatus> StatusMapping = new Dictionary<string, OrderStatus>
        {
            { "未审核", OrderStatus.Unreviewed },
            { "驳回", OrderStatus.Rejected },
            { "用户撤回", OrderStatus.WithdrawnByUser },
            { "等待分配", OrderStatus.WaitingForAssignment },
            { "等待维修", OrderStatus.WaitingForRepair },
            { "已被维修", OrderStatus.Repaired }
        };

        public static OrderStatus MapStatus(string chineseStatus)
        {
            if (StatusMapping.TryGetValue(chineseStatus, out var orderStatus))
            {
                return orderStatus;
            }
            else
            {
                throw new ArgumentException($"Unknown order status: {chineseStatus}");
            }
        }
    }
}